<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 15/8/8
 * Time: 下午9:44
 */
class ModGameLogin
{
    public function getData($params){
        $where = ' WHERE RecDate BETWEEN :t_s AND :t_e
                        AND Period=:period
                        AND GameSrc=:g ';

        $args = array(
            ':t_s'    => $params['time_start'],
            ':t_e'    => $params['time_end'],
            ':period' => 'Daily',
            ':g'	  => $params['g'],
        );
        
        $user = AccAuth::me();
        if($params['os'] != 'ALL' || $user['IsViewAll'] != 1){
        	$where .= " AND Platform=:p ";
        	$args[':p'] = $params['os'];
        }
        
        if($params['os'] == 'ALL' && $params['server'] == 'ALL'){  //都为all，查询专服下的所有渠道的服务器
        	$modServer = new ModServer();
        	$list = $modServer->getServerForSpecialId($params['g'],$params['special_id']);
        	$where .= " AND ( ";
        	foreach ($list as $key=>$val){
        		$where .= " SID = '{$val['develop_id']}'  or";
        	}
        	$where = substr($where, 0, -2);
        	$where .= " ) ";
        }else{
        	$where .= " AND SID=:ser ";
        	$args[':ser'] = empty($params['server']) ? 'ALL' : $params['server'];
        }

        $table = 'tblGameLogin';

        $sql = "SELECT * FROM {$table} {$where}";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
        $res = $dbLink->query($sql, $args);
        return $res;
    }
    
    
    public function getlevelData($params,$type){
    	$where = ' WHERE RecDate BETWEEN :t_s AND :t_e
                        AND Period=:period
                        AND GameSrc=:g ';
    	 
    	$args = array(
    			':t_s'    => $params['time_start'],
    			':t_e'    => $params['time_end'],
    			':g'      => empty($params['g']) ? 'ALL' : $params['g'],
    			':period' => 'Daily'
    	);
    	
    	$user = AccAuth::me();
    	if($params['os'] != 'ALL' || $user['IsViewAll'] != 1){
    		$where .= " AND Platform=:p ";
    		$args[':p'] = $params['os'];
    		
    		$field = 'recdate,Period,GameSrc,Level,PlayUsers';
    	}else{
    		$field = 'recdate,Period,GameSrc,Level,SUM(PlayUsers) as PlayUsers';
    	}
    	 
    	if($params['os'] == 'ALL' && $params['server'] == 'ALL'){  //都为all，查询专服下的所有渠道的服务器
    		$modServer = new ModServer();
    		$list = $modServer->getServerForSpecialId($params['g'],$params['special_id']);
    		$where .= " AND ( ";
    		foreach ($list as $key=>$val){
    			$where .= " SID = '{$val['develop_id']}'  or";
    		}
    		$where = substr($where, 0, -2);
    		$where .= " ) ";
    	}else{
    		$where .= " AND SID=:ser ";
    		$args[':ser'] = empty($params['server']) ? 'ALL' : $params['server'];
    	}
    	 
    	$table = '';
    	if($type == 1){  //所有用户等级流失
    		$table = 'tblgamealllevel';
    	}else if($type == 2){ //新用户等级流失
    		$table = 'tblgamenaulevel';
    	}else if($type == 3){  //活跃用户等级流失
    		$table = 'tblgameactivelevel';
    	}
    	
    	$sql = "SELECT {$field} FROM {$table} {$where} GROUP BY recdate,Period,GameSrc,Level ORDER BY recdate,level";
    	$dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
    	$res = $dbLink->query($sql, $args);
    	 
    	return $res;
    }
    
    
    public function getUserData($params,$type){
    	$where = ' WHERE RecDate BETWEEN :t_s AND :t_e
                        AND Period=:period
                        AND GameSrc=:g ';
    	
    	$args = array(
    			':t_s'    => $params['time_start'],
    			':t_e'    => $params['time_end'],
    			':g'      => empty($params['g']) ? 'ALL' : $params['g'],
    			':period' => 'Daily'
    	);
    	
    	$table = '';
    	$fie = '';
    	if($type == 1){  //所有用户等级流失
    		$fie = 'CumRoleUser';
    		$table = 'tblgamerole';
    	}else if($type == 2){ //新用户等级流失
    		$fie = 'RoleUser';
    		$table = 'tblgamerole';
    	}else if($type == 3){  //活跃用户等级流失
    		$fie = 'LoginUser';
    		$table = 'tblgamelogin';
    	}
    	
    	$user = AccAuth::me();
    	if($params['os'] != 'ALL' || $user['IsViewAll'] != 1){
    		$where .= " AND Platform=:p ";
    		$args[':p'] = $params['os'];	
    		$field = "recdate,Period,Platform,GameSrc,SID,{$fie} as allUser";
    		$group = "";
    	}else{
    		$field = "recdate,Period,GameSrc,SUM({$fie}) as allUser";
    		$group = "GROUP BY recdate,Period,GameSrc";
    	}
    	
    	if($params['os'] == 'ALL' && $params['server'] == 'ALL'){  //都为all，查询专服下的所有渠道的服务器
    		$modServer = new ModServer();
    		$list = $modServer->getServerForSpecialId($params['g'],$params['special_id']);
    		$where .= " AND ( ";
    		foreach ($list as $key=>$val){
    			$where .= " SID = '{$val['develop_id']}'  or";
    		}
    		$where = substr($where, 0, -2);
    		$where .= " ) ";
    	}else{
    		$where .= " AND SID=:ser ";
    		$args[':ser'] = empty($params['server']) ? 'ALL' : $params['server'];
    	}
    	
    	

    	$sql = "SELECT {$field} FROM {$table} {$where} {$group}";
    	
    	$dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
    	$res = $dbLink->query($sql, $args);
    	
    	return $res;
    }
    
    
    
    
    
}